<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
        <a-button preIcon="ant-design:reload-outlined" type="primary" @click="handleRefreshCache">缓存</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon style="fontSize:12px" icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="reload" />
  </div>
</template>
<script lang="ts" name="oauth-client" setup>
import {BasicTable, TableAction} from '/@/components/Table';
import InputForm from './Form.vue';
import {useGlobSetting} from '/@/hooks/setting';
import {useDrawer} from '/@/components/Drawer';
import {columns, searchFormSchema} from './data';
import {batchDeleteOauthClient, deleteOauthClient, pageClient, refreshCache} from './api';
import {useListPage} from '/@/hooks/system/useListPage';
import {useMessage} from '/@/hooks/web/useMessage';
import {Icon} from '/@/components/Icon';
import {useGo} from '/@/hooks/web/usePage';

const go = useGo()
const { createMessage } = useMessage();
const glob = useGlobSetting();
//注册drawer
const [registerDrawer, {openDrawer}] = useDrawer();
// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'oauth-client',
  tableProps: {
    api: pageClient,
    columns: columns,
    formConfig: {
      schemas: searchFormSchema,
    },
    bordered: false,
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

function handleAdd() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

async function handleDelete(record) {
  await deleteOauthClient({ id: record.id }, reload);
}

async function handleRefreshCache() {
  await refreshCache()
  createMessage.success('刷新成功')
}

async function batchHandleDelete() {
  await batchDeleteOauthClient({ ids: selectedRowKeys.value }, reload);
}

function handleDetail(record) {
  openDrawer(true, {
    record,
    isUpdate: true,
    isDetail: true
  });
}

function getActions(record) : any[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      // ifShow: record.sendStatus == 0
    },
    {
      label: '删除',
      ifShow: record.sendStatus != 1,
      color: 'error',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '查看',
      onClick: handleDetail.bind(null, record),
    }
  ]
}

function getDropDownAction(record) {
  return [
    
  ]
}
</script>
